package com.inkrl.cart.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inkrl.cart.mapper.CartMapper;
import com.jt.common.pojo.CartItem;
import com.jt.common.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired(required = false)
    private CartMapper cartMapper;

    @Autowired
    private StringRedisTemplate template;
    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public List<Map<String, Object>> queryCart(String token) {
        Map<String,Object> map;
        ValueOperations<String,String> opsForValue = template.opsForValue();
        if (template.hasKey(token)){
            try {
                String userJson = opsForValue.get(token);
                User users = mapper.readValue(userJson,User.class);
                List<Map<String, Object>> cart = cartMapper.selectByUserid(users.getPhone());
                map = new HashMap<String,Object>();
                map.put("resCode",0200);
                cart.add(map);
                return cart;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else {
            List<Map<String, Object>> cart = new ArrayList<>();
            map = new HashMap<String,Object>();
            map.put("resCode",1406);
            cart.add(map);
            return cart;
        }
        return null;
    }

    @Override
    public int addCart(String token, String isbn, Integer count) {
        ValueOperations<String,String> opsForValue = template.opsForValue();
        if (template.hasKey(token)){
            String userJson = opsForValue.get(token);
            try {
                User users = mapper.readValue(userJson,User.class);
                CartItem cartItem = cartMapper.selectByIsbnandUserId(isbn, users.getPhone());
                if (cartItem!=null){
                    cartMapper.updateByIsbnandUserId(isbn,count, users.getPhone());
                }
                cartMapper.insertCart(isbn,count,users.getPhone());
                return 0200;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }else {
            return 1406;
        }
        return 0;
    }

    @Override
    public int deleteCart(String rids) {
        int i = cartMapper.deleteByRids(rids);
        if (i>0){
            return 0200;
        }
        return 1406;
    }

    @Override
    public void cartNum(String itemId, String num) {
        int rid = Integer.parseInt(itemId);
        int count = Integer.parseInt(num);
        cartMapper.updateByItemIdNum(rid,count);
    }
}
